package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.common.model.Option;
import com.settlement.system.model.entity.BsOmsReturnOrder;
import com.settlement.system.mapper.BsOmsReturnOrderMapper;
import com.settlement.system.model.entity.BsOmsReturnOrderDetail;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.service.BsOmsReturnOrderDetailService;
import com.settlement.system.service.BsOmsReturnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.service.SysDictService;
import com.settlement.system.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.BsOmsReturnOrderQuery;
import com.settlement.system.model.form.BsOmsReturnOrderForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.BsOmsReturnOrderImportVo;

/**
 * <p>
 * 百盛退货单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-09-06
 */
@Service
@RequiredArgsConstructor
public class BsOmsReturnOrderServiceImpl extends ServiceImpl<BsOmsReturnOrderMapper, BsOmsReturnOrder> implements BsOmsReturnOrderService {

    private final SysStoreService storeService;

    private final SysDictService dictService;

    private final BsOmsReturnOrderDetailService bsOmsReturnOrderDetailService;
    @Override
    public Page<BsOmsReturnOrder> getPage(BsOmsReturnOrderQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<BsOmsReturnOrder> page = new Page<>(pageNum, pageSize);
        Page<BsOmsReturnOrder> mapperPage = this.baseMapper.getPage(page, queryParams);
        if(mapperPage.getRecords().size()>0){
            SysStore store;
            List<Option> optionList = dictService.listDictOptions("bsReturnOrderStatus");
            List<Option> optionGoodsList = dictService.listDictOptions("bsReturnOrderGoodsStatus");
            Option option;
            List<BsOmsReturnOrderDetail> detailList;
            for (BsOmsReturnOrder record : mapperPage.getRecords()) {
                store = storeService.getByInnerSystemStoreCode(record.getStoreCode());
                if(Objects.nonNull(store)){
                    record.setStoreName(store.getInnerSystemStoreName());
                }
                option = optionList.stream().filter(item -> Objects.equals(item.getValue(), record.getStatus())).findFirst().orElse(null);
                if(Objects.nonNull(option)){
                    record.setStatus(option.getLabel());
                }
                option = optionGoodsList.stream().filter(item -> Objects.equals(item.getValue(), record.getGoodStatus())).findFirst().orElse(null);
                if(Objects.nonNull(option)){
                    record.setGoodStatus(option.getLabel());
                }
                detailList = bsOmsReturnOrderDetailService.list(new LambdaQueryWrapper<BsOmsReturnOrderDetail>().eq(BsOmsReturnOrderDetail::getOId, record.getId()));
                record.setDetailList(detailList);
            }
        }
        return mapperPage;
    }

     @Override
     public boolean saveBsOmsReturnOrder(BsOmsReturnOrderForm bsOmsReturnOrderForm) {
         BsOmsReturnOrder bsOmsReturnOrder = new BsOmsReturnOrder();
         BeanUtils.copyProperties(bsOmsReturnOrderForm, bsOmsReturnOrder);
         bsOmsReturnOrder.setCreateTime(null);
         return this.saveOrUpdate(bsOmsReturnOrder);
     }

     @Override
     public BsOmsReturnOrderForm getBsOmsReturnOrderFormData(Long id) {
         BsOmsReturnOrder bsOmsReturnOrder = this.getById(id);
         BsOmsReturnOrderForm bsOmsReturnOrderForm = new BsOmsReturnOrderForm();
         BeanUtils.copyProperties(bsOmsReturnOrder, bsOmsReturnOrderForm);
         return bsOmsReturnOrderForm;
     }

     @Override
     public boolean deleteBsOmsReturnOrder(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<BsOmsReturnOrderImportVo> voList)  {
        List<BsOmsReturnOrder> bsOmsReturnOrderList = new ArrayList<>();
        for (BsOmsReturnOrderImportVo importVo : voList) {
            BsOmsReturnOrder bsOmsReturnOrder = new BsOmsReturnOrder();
            BeanUtils.copyProperties(importVo, bsOmsReturnOrder);
            bsOmsReturnOrderList.add(bsOmsReturnOrder);
        }
        this.saveOrUpdateBatch(bsOmsReturnOrderList);
    }

    @Override
    public List<BsOmsReturnOrderImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<String> ids = Arrays.stream(idsStr.split(",")).collect(Collectors.toList());
        List<BsOmsReturnOrder> bsOmsReturnOrderList = this.getBaseMapper().selectBatchIds(ids);
        List<BsOmsReturnOrderDetail> orderDetails = bsOmsReturnOrderDetailService.list(new LambdaQueryWrapper<BsOmsReturnOrderDetail>().in(BsOmsReturnOrderDetail::getOId, ids));
        List<BsOmsReturnOrderImportVo> voList = new ArrayList<>();
        BsOmsReturnOrderDetail detail;
        SysStore store;
        for (BsOmsReturnOrder bsOmsReturnOrder : bsOmsReturnOrderList) {
            BsOmsReturnOrderImportVo vo = new BsOmsReturnOrderImportVo();
            BeanUtils.copyProperties(bsOmsReturnOrder, vo);
            // 线上申请金额
            vo.setBuyerApplyRefund(bsOmsReturnOrder.getBuyerApplyRefund().toString());
            // 线上退运费
            vo.setShopFreight(bsOmsReturnOrder.getShopFreight().toString());
            // 优惠/差额
            vo.setFreeAmount(bsOmsReturnOrder.getFreeAmount().toString());
            // 卖家应退金额
            vo.setRefund(bsOmsReturnOrder.getRefund().toString());
            // 买家应补偿金额
            vo.setPayment(bsOmsReturnOrder.getPayment().toString());
            // 实际应退金额
            vo.setActBackAmount(bsOmsReturnOrder.getActBackAmount().toString());
            // 商品应退金额
            vo.setGoodsBackAmount(bsOmsReturnOrder.getGoodsBackAmount().toString());
            // 换货金额
            vo.setTransferAmount(bsOmsReturnOrder.getTransferAmount().toString());
            // 卖家应补
            vo.setUrefund(bsOmsReturnOrder.getUrefund().toString());
            // 最后确认日期
            if(Objects.nonNull(bsOmsReturnOrder.getConfirmDate())){
                vo.setConfirmDate(bsOmsReturnOrder.getConfirmDate().toString().replace("T", " "));
            }
            // 登记时间
            if(Objects.nonNull(bsOmsReturnOrder.getCreated())){
                vo.setCreated(bsOmsReturnOrder.getCreated().toString().replace("T", " "));
            }
            // 申请日期
            if(Objects.nonNull(bsOmsReturnOrder.getAsDate())){
                vo.setAsDate(bsOmsReturnOrder.getAsDate().toString().replace("T", " "));
            }
            detail = orderDetails.stream().filter(x -> Objects.equals(x.getOId(), bsOmsReturnOrder.getId())).findFirst().orElse(null);
            if(Objects.nonNull(detail)){
                BeanUtils.copyProperties(detail, vo);
                // 申请数量
                vo.setQty(detail.getQty().toString());
                // 退货数量
                vo.setRQty(detail.getRQty().toString());
                // 线上明细金额
                vo.setShopAmount(detail.getShopAmount().toString());
                // 单价
                vo.setPrice(detail.getPrice().toString());
                // 申请金额
                vo.setAmount(detail.getAmount().toString());
            }
            store = storeService.getByInnerSystemStoreCode(bsOmsReturnOrder.getStoreCode());
            if(Objects.nonNull(store)){
                vo.setStoreName(store.getInnerSystemStoreName());
            }
            voList.add(vo);
        }
        return voList;
    }
}
